package com.habdqn.easybuy.web.front.shop;

import com.habdqn.easybuy.entity.Shop;
import com.habdqn.easybuy.entity.User;
import com.habdqn.easybuy.service.ShopService;
import com.habdqn.easybuy.service.impl.ShopServiceImpl;
import com.habdqn.easybuy.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Xing.Jiejian
 * @date 2020/12/2
 */
@WebServlet(name = "ShopUpdateServlet",urlPatterns = "/shop/update")
public class UpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取购物车记录编号
        String strShopId = request.getParameter("id");
        if (StringUtils.isNotBlank(strShopId)){
            int shopId = Integer.parseInt(strShopId);
            ShopService shopService = new ShopServiceImpl();
            // 根据购物车编号进行更新
            String action = request.getParameter("action");
            switch (action){
                case "jia":
                    shopService.plusNumber(shopId,1);
                    break;
                case "jian":
                    shopService.plusNumber(shopId,-1);
                    break;
                case "closeText":
                    String strValue = request.getParameter("value");
                    if (StringUtils.isNotBlank(strValue)) {
                        shopService.updateNumber(shopId,Integer.parseInt(strValue));
                    }
                    break;
                case "delText":
                    shopService.deleteById(shopId);
                    break;
                default:
            }
        }
        response.sendRedirect(request.getContextPath()+"/shop");
    }
}
